package com.itheima.servlet.night;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ServletContextDemo01 extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = getServletContext();

        //根据名称获取全局配置的参数
        String value = context.getInitParameter("globalEncoding");
        System.out.println("value = " + value);
        //获取虚拟目录
        String contextPath = context.getContextPath();
        System.out.println("contextPath = " + contextPath);
        //根据虚拟目录获取应用部署的磁盘绝对路径（后期是帮助我们获取文件的绝对路径的）
        String realPath = context.getRealPath("/");
        System.out.println("realPath = " + realPath);

        String bPath = context.getRealPath("/b.txt");
        System.out.println("bPath = " + bPath);

        String cPath = context.getRealPath("/WEB-INF/c.txt");
        System.out.println("cPath = " + cPath);
        String aPath = context.getRealPath("/WEB-INF/classes/c.txt");
        System.out.println("aPath = " + aPath);

        //设置共享数据
        context.setAttribute("username","zhangsan");

        //删除共享数据
        context.removeAttribute("username");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
